<template>
  <div class="contact-list">
    <div v-for="contact in contacts" :key="contact.id"
      :class="['contact-item', contact.id === selectedId ? 'active' : '']"
      @click="useContact.selectContact(contact.id)">
      <div class="avatar">
        <img v-if="contact.avatar" :src="contact.avatar" :alt="contact.name" />
        <span v-else class="avatar-text">{{ contact.name.charAt(0) }}</span>
      </div>
      <div class="info">
        <div class="name">{{ contact.name }}</div>
        <div class="desc">{{ contact.desc }}</div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useContactStore } from '@/stores/contact'
import { storeToRefs } from 'pinia';

const useContact = useContactStore();
const { contacts, selectedId } = storeToRefs(useContact);
</script>

<style scoped>
.contact-list {
  height: 100%;
  width: 220px;
  border-radius: 8px 0 0 8px;
  border: 1px solid #d5d5d5;
  box-sizing: border-box;
  padding: 0;
}

.contact-item {
  background-color: #f7f7f7;
  display: flex;
  align-items: center;
  padding: 10px 18px;
  box-sizing: border-box;
  cursor: pointer;
  transition: background 0.2s;
}

.contact-item:hover {
  background-color: #eaeaea;
}

.contact-item.active {
  background: #e6f7ff;
}

.contact-item:hover {
  background: #f2f3f5;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: #eee;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  overflow: hidden;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar-text {
  font-size: 16px;
  color: #fff;
  background: #409eff;
  font-weight: bold;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}

.info {
  flex: 1;
}

.name {
  font-size: 15px;
  font-weight: 500;
}

.desc {
  font-size: 12px;
  color: #888;
}
</style>
